/*****************************************************************************
 *                                                                           *
 *   Copyright (C) 2010 by Luca Cireddu                                      *
 *                         luca@lucacireddu.it                               *
 *                         http://www.lucacireddu.it                         *
 *                                                                           *
 *   This program is free software; you can redistribute it and/or modify    *
 *   it under the terms of the GNU General Public License as published by    *
 *   the Free Software Foundation; either version 2 of the License.          *
 *                                                                           *
 *   This program is distributed in the hope that it will be useful,         *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
 *   GNU General Public License for more details.                            *
 *                                                                           *
 *   You should have received a copy of the GNU General Public License       *
 *   along with this program; if not, write to the                           *
 *   Free Software Foundation, Inc.                                          *
 *   51 Franklin Street, Fifth Floor                                         *
 *   Boston, MA  02110-1301, USA.                                            *
 *                                                                           *
 *****************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>

#include "common.h"
#include "stringsutils.h"
#include "defines.h"
#include "ngexit.h"

char *ltrimc(char *input, char sep)
{
    int i, j, ln;
    char *ret;

    i = 0;

    while(*(input + i) == sep)
        i++;

    if(i == 0)
        return input;

    ln = strlen(input);

    ret = (char *) malloc((ln - i + 1) * sizeof(char));

    for(j = 0; j < (ln - i); j++)
        *(ret + j) = *(input + j + i);

    return ret;
}

char *rtrimc(char *input, char sep)
{
    int i, j, ln;
    char *ret;

    i = 0;

    ln = strlen(input);

    while(*(input + (ln-1) - i) == sep)
        i++;

    if(i == 0)
        return input;

    ret = (char *) malloc((ln - i + 1) * sizeof(char));

    for(j = 0; j < (ln - i); j++)
        *(ret + j) = *(input + j);

    *(ret + j) = '\0';

    return ret;
}

char *trimc(char *input, char sep)
{
    return ltrimc(rtrimc(input, sep), sep);
}

char *ltrim(char *input)
{
    return ltrimc(input, ' ');
}

char *rtrim(char *input)
{
    return rtrimc(input, ' ');
}

char *trim(char *input)
{
    return trimc(input, ' ');
}

char **explode(char *input, char sep)
{
    char **ret;
    char *dest;
    char c;
    int ln, i, j, k;

    ret = (char **) malloc(sizeof(char *));
    dest = (char *) malloc(sizeof(char));
    ret[0] = dest;

    j = 0;
    k = 0;

    if(input != NULL) {
        ln = strlen(input);
        for(i=0; i<ln; i++) {
            if(*(input+i) == sep) {
                *(dest + j) = '\0';
                j = 0;
                *(ret + k) = dest;
                k++;
                ret = (char **) realloc(ret, (k+1) * sizeof(char *));
                dest = (char *) malloc(sizeof(char));
            } else {
                dest = (char *) realloc(dest, (j+2) * sizeof(char));
                c = *(input + i);
                if(c == '\0')
                    c = '0';
                *(dest + j) = *(input + i);
                j++;
            }
        }
    }

    *(dest + j) = '\0';
    *(ret + k) = dest;

    ret = (char **) realloc(ret, (k+1) * sizeof(char *));
    *(ret + (k+1)) = NULL;

    return ret;
}

void explodeFree(char **input)
{
    int ln, i;

    ln = explodeSize(input);

    for(i = 0; i < ln; i++)
        free(input[i]);

    for(i = ln; i > 0; i--)
        free(input[i - 1]);
}

char *remDouble(char *input, char rem)
{
    char *ret;
    int ln, i, d, p;

    if(input == NULL) {
        ret = (char *) malloc(sizeof(char));
        ret = '\0';
        return ret;
    }

    ln = strlen(input);
    d = ln;
    ret = (char *) malloc((ln + 1) * sizeof(char));

    for(i = 0, p = 0; i < (ln-1); i++, p++) {
        if(*(input + i) == rem) {
            if(*(input + (i+1)) == *(input + i)) {
                d--;
                p--;
                ret = (char *) realloc(ret, (d + 1) * sizeof(char));
            } else
                *(ret + p) = *(input + i);
        } else
            *(ret + p) = *(input + i);
    }

    *(ret + p) = *(input + i);
    *(ret + (p + 1)) = '\0';

    return ret;
}

char *remDoubleSpaces(char *input)
{
    return remDouble(input, ' ');
}

char *cleanString(char *input)
{
    return remDoubleSpaces(trim(input));
}

void printArray(char **input)
{
    int i;

    i = 0;

    while(input[i] != NULL) {
        printf("array[%d] = %s\n", i, input[i]);
        i++;
    }
}

int explodeSize(char **input)
{
    int ret;
    int i;

    i = 0;
    ret = 0;

    do {
        i++;
    } while(input[i - 1] != NULL);

    return ret;
}
